﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections;

namespace Xceed.Silverlight.DataGrid
{
  internal class ReadOnlyListArrayWrapper<T> : IList<T>
  {
    public ReadOnlyListArrayWrapper( T[] array )
    {
      m_array = array;
    }

    #region IList<T> Members

    public int IndexOf( T item )
    {
      return Array.IndexOf( m_array, item );
    }

    public void Insert( int index, T item )
    {
      throw new NotSupportedException( "Cannot insert items into a read-only list." );
    }

    public void RemoveAt( int index )
    {
      throw new NotSupportedException( "Cannot remove items from a read-only list." );
    }

    public T this[ int index ]    {
      get
      {
        return m_array[ index ];
      }
      set
      {
        throw new NotSupportedException( "Cannot modify a read-only list." );
      }
    }

    #endregion

    #region ICollection<T> Members

    public void Add( T item )
    {
      throw new NotSupportedException( "Cannot add items into a read-only list." );
    }

    public void Clear()
    {
      throw new NotSupportedException( "Cannot clear a read-only list." );
    }

    public bool Contains( T item )
    {
      return ( Array.IndexOf( m_array, item ) != -1 );
    }

    public void CopyTo( T[] array, int arrayIndex )
    {
      int length = m_array.GetLength(0) - arrayIndex;

      Array.Copy( m_array, arrayIndex, array, 0, length );
    }

    public int Count
    {
      get
      {
        return m_array.GetLength( 0 );
      }
    }

    public bool IsReadOnly
    {
      get
      {
        return true;
      }
    }

    public bool Remove( T item )
    {
      throw new NotSupportedException( "Cannot remove items from a read-only list." );
    }

    #endregion

    #region IEnumerable<T> Members

    public IEnumerator<T> GetEnumerator()
    {
      return new TypedArrayEnumerator<T>( m_array );
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
      return m_array.GetEnumerator();
    }

    #endregion

    private T[] m_array;

    #region Sub Classes

    private class TypedArrayEnumerator<U> : IEnumerator<U>
    {
      public TypedArrayEnumerator( U[] sourceArray )
      {
        if( sourceArray == null )
          throw new ArgumentNullException( "sourceArray" );

        m_enumerator = sourceArray.GetEnumerator();
      }

      #region IEnumerator<T> Members

      public U Current
      {
        get
        {
          return (U)m_enumerator.Current ;
        }
      }

      #endregion

      #region IDisposable Members

      public void Dispose()
      {
        IDisposable disposable = m_enumerator as IDisposable;

        if( disposable != null )
        {
          disposable.Dispose();
        }
      }

      #endregion

      #region IEnumerator Members

      object System.Collections.IEnumerator.Current
      {
        get
        {
          return m_enumerator.Current;
        }
      }

      public bool MoveNext()
      {
        return m_enumerator.MoveNext();
      }

      public void Reset()
      {
        m_enumerator.Reset();
      }

      #endregion

      IEnumerator m_enumerator;
    }

    #endregion
  }
}
